<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            label="媒体文件类型"
            prop="type"
            :rules="[{ required: true, message: '请选择媒体文件类型', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.type" placeholder="请选择媒体文件类型" style="width: 100%">
              <el-option
                v-for="item in mediaList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="消息类型"
            prop="noticeType"
            :rules="[{ required: true, message: '请选择消息类型', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.noticeType" placeholder="请选择消息类型" style="width: 100%">
              <el-option
                v-for="item in noticeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="上传文件"
            prop="fileName"
            :rules="[{ required: true, message: '请上传文件', trigger: 'change' }]"
          >
            <el-upload
              class="upload-demo"
              :action="baseURL+'rest/api/upload/sfile'"
              :on-preview="handlePreview"
              :on-success="handleSuccess"
              :before-remove="beforeRemove"
              :on-exceed="handleExceed"
              multiple
              :file-list="fileList"
              :limit="1"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">上传文件大小不能超过10MB!</div>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="当前状态"
            prop="state"
            :rules="{ required: true, message: '请选择当前状态', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.state" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="消息类型"
            prop="px"
            :rules="[{ required: true, message: '请选择消息类型', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-input-number v-model="form.px" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateObject, saveObject } from '@/api/ding/index'
import { queryAllNoticeType, queryAllMediaType } from '@/api/ding/zd.js'
import { deleteObject } from '@/api/portal/manager/layout'
export default {
  name: 'DingTalkMediaEntity',
  data() {
    return {
      title: '数据项管理',
      loading: false,
      dialogFormVisible: false,
      noticeList: [],
      mediaList: [],
      fileList: [],
      baseURL: process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_API : '/', // api 的 base_url
      status: [{ id: 0, value: false, label: '禁用' },
        { id: 1, value: true, label: '启用' }],
      form: {
        id: null,
        type: '',
        noticeType: '',
        fileName: '',
        url: '',
        path: '',
        mediaId: '',
        state: true,
        px: 0
      }
    }
  },
  methods: {
    openEntity(data) {
      this.queryData = null
      this.fileList = []
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改信息'
        this.dialogFormVisible = true
        const item = {
          name: this.form.fileName,
          url: this.form.url,
          path: this.form.path
        }
        this.fileList.push(item)
      } else {
        this.title = '新增数据项信息'
        this.dialogFormVisible = true
      }
      this.initMedia()
      this.initNoticeType()
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            this.form.pzx = JSON.stringify(this.jxx)
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            this.form.pzx = JSON.stringify(this.jxx)
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    initMedia() {
      queryAllMediaType().then(res => {
        const { data } = res
        if (undefined !== data) {
          this.mediaList = data.data
        }
      }).catch(() => {
        this.mediaList = []
      })
    },
    initNoticeType() {
      queryAllNoticeType().then(res => {
        const { data } = res
        if (undefined !== data) {
          this.noticeList = data.data
        }
      }).catch(() => {
        this.noticeList = []
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    handlePreview(file) {
      console.log(file)
    },
    handleSuccess(response, file, fileList) {
      this.fileList = [{
        id: file.response.id,
        name: file.response.name,
        url: file.response.url,
        path: file.response.path
      }]
      this.form.fileName = file.response.name
      this.form.url = file.response.url
      this.form.path = file.response.path
    },
    handleExceed(files, fileList) {
    },
    beforeRemove(file, fileList) {
      this.$confirm('此操作将永久删除' + file.name + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.fileName = null
        this.form.url = null
        this.form.path = null
      }).catch(() => {
        this.$message.success('取消删除成功！')
      })
    },
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过10MB!')
      }
      return isLt2M
    }
  }
}
</script>

<style scoped>

</style>
